<?php
namespace Admin\Controller;
class FriendsController extends AdminController {
	public function index(){
        $name       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($name)){
            $map['id|name']=   array(intval($name),array('like','%'.$name.'%'),'_multi'=>true);
        }else{
            $map['name']    =   array('like', '%'.(string)$name.'%');
        }

        $list   = $this->lists('friends', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '用户信息';
        $this->display();
    }
    public function add(){
    	if(IS_POST){
    		$friend=D('Friends');
    		$res=$friend->create();
    		if($res){
    			if($friend->add()){
    				$this->success('添加成功');
    			}else{
    				$this->error('添加失败');
    			}

    		}else{
    			$this->error($friend->getError());
    		}
    	}else{
    		$this->meta_title = '新增友链';
    		$this->display();
    	}
    }
    public function update(){
        if(IS_POST){
            $res=D('friends')->create();
            if($res){
                if($friend=D('friends')->save()){
                    $this->success('编辑成功');
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($friend->getError());
            }
        }else{
            $this->meta_title = '修改友链';
            $friend = D('friends')->find(I('get.id'));
            $this->assign('res',$friend);
            $this->display(); 
        }
    }
    public function setStatus($model='Friends'){
        return parent::setStatus($model);
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('friends', $map );
                break;
            case 'resumeuser':
                $this->resume('friends', $map );
                break;
            case 'deleteuser':
                $this->delete('friends', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}
